//
//  LFDBManager.swift
//  1604_LimitFree
//
//  Created by 千锋 on 16/9/29.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import FMDB
class LFDBManager: NSObject {
    //数据库对列
    private var myQueue:FMDatabaseQueue?
    
    //获取单利
    class var sharedManager:LFDBManager{
        
        struct Static {
            static var onceToken:
                dispatch_once_t = 0
            static var instance: LFDBManager? = nil
        }
        dispatch_once(&Static.onceToken){
            Static.instance = LFDBManager()
        }
        return Static.instance!
    }
    
    override init() {
        
        //文件路径
        let cacheDir = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).last
        let path = cacheDir?.stringByAppendingString("/collect.db")
        
            //初始化数据库
        myQueue = FMDatabaseQueue(path: path)
        //创建表格
        myQueue?.inDatabase({ (database) in
            let createSql = "create table if not exists collect (collectId integer primary key autoincrement,applicationId varchar(255), name varchar(255), headImage blob)"
            let flag = database.executeUpdate(createSql, withArgumentsInArray: nil)
            if flag != true {
                print(database.lastErrorMessage())
            }
        })
        
    }
    //收藏
    func addCollect(model:CollectModel,finishClosure:(Bool -> Void)){
        
        myQueue?.inDatabase({ (database) in
            let insertSql = "insert into collect (applicationId,name,headImage) values (?,?,?)"
            let data = UIImagePNGRepresentation(model.headImage!)
            
            let flag = database.executeUpdate(insertSql, withArgumentsInArray: [model.applicationId!,model.name!,data!])
            
            if flag != true {
                print(database.lastErrorMessage())
            }
            finishClosure(flag)
        })
    }
    
    //判断某个应用是否已收藏］
    func isAppFavorite(appId:String,resultClosure:(Bool->Void)){
        myQueue?.inDatabase({ (database) in
            let sql = "select * from collect where applicationId = ?"
            let rs = database.executeQuery(sql, withArgumentsInArray: [appId])
            if rs.next(){
                //已经收藏
                resultClosure(true)
               
            }else{
                //还没有收藏
                resultClosure(false)
            }
            rs.close()
        })
    }
    //查询
    func searchAllCollectData(resultClosure:(Array<CollectModel>->Void)){
        myQueue?.inDatabase({ (database) in
            let sql = "select * from collect"
            
            let rs = database.executeQuery(sql, withArgumentsInArray: nil)
            var tmpArray = Array<CollectModel>()
            while rs.next(){
                let model = CollectModel()
                model.collectId = Int(rs.intForColumn("collectId"))
                model.applicationId = rs.stringForColumn("applicationId")
                model.name = rs.stringForColumn("name")
                let data = rs.dataForColumn("headImage")
                model.headImage = UIImage(data: data)
                tmpArray.append(model)
            }
            resultClosure(tmpArray)
            rs.close()
        })
    }
    //删除
    func deleteWithAppId(appId:String,resultClosure:(Bool->Void)){
        myQueue?.inDatabase({ (database) in
            let sql = "delete from collect where applicationId = ?"
            let flag = database.executeUpdate(sql, withArgumentsInArray: [appId])
            if flag == false{
                print(database.lastErrorMessage())
            }
            resultClosure(flag)
        })
        
    }

}
